package com.sls.game.parser.enfu;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.enfu.EnfuAreaInfo;
import com.sls.game.pojo.enfu.EnfuGoodsDetailInfo;
import com.sls.game.pojo.enfu.EnfuServerInfo;

/**
 * 
 * @Description： 商品详细解析
 * @author Wangxuegang
 * @Date：2013-5-22
 * 
 */
public class EnfuGoodsDetailResponse {

	private static Logger log = Logger.getLogger(EnfuGoodsDetailResponse.class);

	public static Map<String, Object> parseGoodsDetail(String xmlStr) {
		EnfuGoodsDetailInfo info = new EnfuGoodsDetailInfo();
		String productid = null;
		String productname = null;
		String gamename = null;
		String gameid = null;
		String areaname = null;
		String areaid = null;
		String producttype = null;
		String isarea = null;
		Map<String, Object> map = new HashMap<String, Object>();
		// 创建游戏区域对象List
		List<EnfuAreaInfo> areaInfoList = new ArrayList<EnfuAreaInfo>();
		// 创建游戏服务器对象List
		List<EnfuServerInfo> srvInfoList = new ArrayList<EnfuServerInfo>();
		// 创建游戏具体游戏对象List
		List<EnfuGoodsDetailInfo> gameInfoList = new ArrayList<EnfuGoodsDetailInfo>();
		List<EnfuGoodsDetailInfo> productinfoList = new ArrayList<EnfuGoodsDetailInfo>();
		String resultcode = null;
		String resultdescription = null;

		InputSource source = null;
		StringReader read = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);
			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("apiresult".equals(node.getNodeName())) { // 第二层节点
					Element apiresultNode = (Element) node.getChildNodes();
					NodeList apiresultNodes = apiresultNode.getChildNodes();
					for (int j = 0; j < apiresultNodes.getLength(); j++) {
						Node grandsonNode = apiresultNodes.item(j);
						if ("resultcode".equals(grandsonNode.getNodeName())) {
							resultcode = grandsonNode.getTextContent();
							info.setResultcode(resultcode);
						} else if ("resultdescription".equals(grandsonNode
								.getNodeName())) {
							resultdescription = grandsonNode.getTextContent();
							info.setResultdescription(resultdescription);
						}
					}
				} else if ("productinfo".equals(node.getNodeName())) {
					Element productinfoNode = (Element) node.getChildNodes();
					NodeList productinfoNodes = productinfoNode.getChildNodes();
					for (int j = 0; j < productinfoNodes.getLength(); j++) {
						Node grandsonNode = productinfoNodes.item(j);
						if ("productid".equals(grandsonNode.getNodeName())) {
							productid = grandsonNode.getTextContent();
							info.setProductid(productid);
						} else if ("productname".equals(grandsonNode
								.getNodeName())) {
							productname = grandsonNode.getTextContent();
							info.setProductname(productname);
						} else if ("producttype".equals(grandsonNode
								.getNodeName())) {
							producttype = grandsonNode.getTextContent();
							info.setProducttype(producttype);
						} else if ("isarea".equals(grandsonNode.getNodeName())) {
							isarea = grandsonNode.getTextContent();
							info.setIsarea(isarea);
						}
					}
					productinfoList.add(info);
					map.put("productinfoList", productinfoList);
				} else if ("games".equals(node.getNodeName())) {
					Element gamesNode = (Element) node.getChildNodes();
					NodeList gamesNodes = gamesNode.getChildNodes();
					for (int j = 0; j < gamesNodes.getLength(); j++) {
						Node grandsonNode = gamesNodes.item(j);
						// 创建具体游戏对象
						EnfuGoodsDetailInfo gameInfo = new EnfuGoodsDetailInfo();
						gameInfo.setProductid(productid);
						gameInfo.setProductname(productname);
						gameInfo.setProducttype(producttype);
						gameInfo.setIsarea(isarea);
						if ("game".equals(grandsonNode.getNodeName())) {
							Element gameInfoNode = (Element) grandsonNode
									.getChildNodes();
							NodeList gameInfoNodes = gameInfoNode
									.getChildNodes();
							for (int k = 0; k < gameInfoNodes.getLength(); k++) {
								Node gameNode = gameInfoNodes.item(k);
								if ("gamename".equals(gameNode.getNodeName())) {
									gamename = gameNode.getTextContent();
									gameInfo.setGamename(gamename);
								} else if ("gameid".equals(gameNode
										.getNodeName())) {
									gameid = gameNode.getTextContent();
									gameInfo.setGameid(gameid);
								} else if ("buynumberlist".equals(gameNode
										.getNodeName())) {
									String buynumberlist = gameNode
											.getTextContent();
									gameInfo.setBuynumberlist(buynumberlist);
								} else if ("buynumberuntil".equals(gameNode
										.getNodeName())) {
									String buynumberuntil = gameNode
											.getTextContent();
									gameInfo.setBuynumberuntil(buynumberuntil);
								} else if ("chargetypename".equals(gameNode
										.getNodeName())) {
									String chargetypename = gameNode
											.getTextContent();
									gameInfo.setChargetypename(chargetypename);
								} else if ("chargetypid".equals(gameNode
										.getNodeName())) {
									String chargetypeid = gameNode
											.getTextContent();
									gameInfo.setChargetypeid(chargetypeid);
								} else if ("accounttypename".equals(gameNode
										.getNodeName())) {
									String accounttypename = gameNode
											.getTextContent();
									gameInfo.setAccounttypename(accounttypename);
								} else if ("accounttypeid".equals(gameNode
										.getNodeName())) {
									String accounttypeid = gameNode
											.getTextContent();
									gameInfo.setAccounttypeid(accounttypeid);
								} else if ("areas".equals(gameNode
										.getNodeName())) {
									Element areasNode = (Element) gameNode
											.getChildNodes();
									NodeList areasNodes = areasNode
											.getChildNodes();
									for (int l = 0; l < areasNodes.getLength(); l++) {
										Node areaInfoNode = areasNodes.item(l);
										// 创建区域对象
										EnfuAreaInfo areaInfo = new EnfuAreaInfo();
										areaInfo.setProductid(productid);
										areaInfo.setGameid(gameid);
										areaInfo.setGamename(gamename);
										if ("area".equals(areaInfoNode
												.getNodeName())) {
											Element areaNode = (Element) areaInfoNode
													.getChildNodes();
											NodeList areaNodes = areaNode
													.getChildNodes();
											for (int m = 0; m < areaNodes
													.getLength(); m++) {
												Node areaChildNode = areaNodes
														.item(m);
												if ("name".equals(areaChildNode
														.getNodeName())) {
													areaname = areaChildNode
															.getTextContent();
													areaInfo.setAreaname(areaname);
												} else if ("id"
														.equals(areaChildNode
																.getNodeName())) {
													areaid = areaChildNode
															.getTextContent();
													areaInfo.setAreaid(areaid);
												} else if ("srvs"
														.equals(areaChildNode
																.getNodeName())) {
													Element srvsNode = (Element) areaChildNode
															.getChildNodes();
													NodeList srvsNodes = srvsNode
															.getChildNodes();
													for (int n = 0; n < srvsNodes
															.getLength(); n++) {
														Node srvsInfoNode = srvsNodes
																.item(n);
														// 创建游戏服务器对象
														EnfuServerInfo srvInfo = new EnfuServerInfo();
														srvInfo.setProductid(productid);
														srvInfo.setGameid(gameid);
														srvInfo.setAreaname(areaname);
														srvInfo.setAreaid(areaid);
														if ("srv"
																.equals(srvsInfoNode
																		.getNodeName())) {
															Element srvNode = (Element) srvsInfoNode
																	.getChildNodes();
															NodeList srvNodes = srvNode
																	.getChildNodes();
															for (int o = 0; o < srvNodes
																	.getLength(); o++) {
																Node srvInfoNode = srvNodes
																		.item(o);
																if ("name"
																		.equals(srvInfoNode
																				.getNodeName())) {
																	String srvname = srvInfoNode
																			.getTextContent();
																	srvInfo.setSrvname(srvname);
																} else if ("id"
																		.equals(srvInfoNode
																				.getNodeName())) {
																	String srvid = srvInfoNode
																			.getTextContent();
																	srvInfo.setSrvid(srvid);
																}
															}
														}
														srvInfoList
																.add(srvInfo);
														map.put("srvInfoList",
																srvInfoList);
													}
												}
											}
										}
										areaInfoList.add(areaInfo);
										map.put("areaInfoList", areaInfoList);
									}
								}
							}
						}
						gameInfoList.add(gameInfo);
						map.put("gameInfoList", gameInfoList);
					}
				}
			}
		} catch (Exception e) {
			log.error(e);
		}

		return map;
	}

	// public static void main(String[] args) {
	// StringBuilder sb = new StringBuilder();
	// sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
	// sb.append("<response>");
	// sb.append("<apiresult>");
	// sb.append("<resultcode>0</resultcode>");
	// sb.append("<resultdescription>查询成功</resultdescription>");
	// sb.append("</apiresult>");
	// sb.append("<productinfo>");
	// sb.append("<productid>101</productid>");
	// sb.append("<productname>空中网一卡通按元充值</productname>");
	// sb.append("<producttype>直充</producttype>");
	// sb.append("<isarea>true</isarea>");
	// sb.append("</productinfo>");
	// sb.append("<games>");
	// sb.append("<game>");
	// sb.append("<gamename>坦克世界</gamename>");
	// sb.append("<gameid>1421</gameid>");
	// sb.append("<buynumberlist>1,2,3,4,5,6,7,8,90,470,480,490,500</buynumberlist>");
	// sb.append("<buynumberuntil>元</buynumberuntil>");
	// sb.append("<areas>");
	// sb.append("<area>");
	// sb.append("<name>电信南方区</name>");
	// sb.append("<id>1500200</id>");
	// sb.append("</area>");
	// sb.append("<area>");
	// sb.append("<name>联通北方区</name>");
	// sb.append("<id>1500100</id>");
	// sb.append("</area>");
	// sb.append("</areas>");
	// sb.append("</game>");
	// sb.append("<game>");
	// sb.append("<gamename>龙</gamename>");
	// sb.append("<gameid>1422</gameid>");
	// sb.append("<buynumberlist>1,2,3,4,5,6,7,8,9,10,11,12,,480,490,500</buynumberlist>");
	// sb.append("<buynumberuntil>元</buynumberuntil>");
	// sb.append("<areas>");
	// sb.append("<area>");
	// sb.append("<name>华南电信二</name>");
	// sb.append("<id>901800</id>");
	// sb.append("<srvs>");
	// sb.append("<srv>");
	// sb.append("<name>朱元璋</name>");
	// sb.append("<id>901807</id>");
	// sb.append("</srv>");
	// sb.append("<srv>");
	// sb.append("<name>方世玉</name>");
	// sb.append("<id>901812</id>");
	// sb.append("</srv>");
	// sb.append("</srvs>");
	// sb.append("</area>");
	// sb.append("</areas>");
	// sb.append("</game>");
	// sb.append("</games>");
	// sb.append("</response>");
	//
	// String xmlStr = sb.toString().trim();
	// Map<String, Object> map = parseGoodsDetail(xmlStr);
	// List<EnfuGoodsDetailInfo> srvInfoList = (List<EnfuGoodsDetailInfo>) map
	// .get("srvInfoList");
	// List<EnfuGoodsDetailInfo> areaInfoList = (List<EnfuGoodsDetailInfo>) map
	// .get("areaInfoList");
	// List<EnfuGoodsDetailInfo> gameInfoList = (List<EnfuGoodsDetailInfo>) map
	// .get("gameInfoList");
	// }

}
